package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 *
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {
    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;
    @Autowired
    private TbClueMapper tbClueMapper;

    /**
     * 新增线索跟进记录
     */
    @Override
    @Transactional
    public ClueTrackRecordVo add(ClueTrackRecordVo tbClueTrackRecord) {
        //修改跟进信息
        TbClue tbClue = new TbClue();
        BeanUtils.copyProperties(tbClueTrackRecord, tbClue);//将tbClueTrackRecord中的信息拷贝到tbClue中
        tbClue.setStatus(TbClue.StatusType.FOLLOWING.getValue());//将线索的状态改为2
        tbClue.setId(tbClueTrackRecord.getClueId());
        tbClueMapper.updateTbClue(tbClue);
        //添加跟进信息
        TbClueTrackRecord record = new TbClueTrackRecord();
        BeanUtils.copyProperties(tbClueTrackRecord, record);
        record.setCreateBy(SecurityUtils.getUsername());//设置跟进人
        record.setCreateTime(DateUtils.getNowDate());//设置创建时间
        tbClueTrackRecordMapper.add(record);
        return tbClueTrackRecord;
    }


    /**
     * 查询线索跟进记录列表
     */
    @Override
    public TableDataInfo list(Long clueId) {
        //调用mapper根据id查询线索跟踪记录
        //
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("成功");
        tableDataInfo.setParams(null);
        List<TbClueTrackRecord> list= tbClueTrackRecordMapper.list(clueId);
        tableDataInfo.setRows(list);
        tableDataInfo.setTotal(list.size());

        return tableDataInfo;
    }


}
